package com.m.crm.web;

import com.m.crm.dto.BaseResult;
import com.m.crm.dto.BootStrapTableResult;
import com.m.crm.entity.Account;
import com.m.crm.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created by yugoh on 2017/5/19.
 */
@RestController
@RequestMapping("/accounts")
public class AccountController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    private final AccountService accountService;

    @Autowired
    public AccountController(AccountService accountService) {
        this.accountService = accountService;
    }

    /**
     * 客户注册
     *
     * @param account
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public BaseResult<Object> register(Account account, String securityCode) {
        log.info("invoke----------/accounts.post");
        try {
            accountService.register(account, securityCode);
        } catch (Exception e) {
            return new BaseResult<>(false, e.getMessage());
        }
        return new BaseResult<>(true, null);
    }

    /**
     * 客户列表查询
     *
     * @param account
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public BootStrapTableResult<Account> list(Account account, Date startTime, Date endTime, Integer offset,
                                              Integer limit) {
        log.info("invoke----------/accounts.get");
        return new BootStrapTableResult<>(accountService.listAccount(account, startTime, endTime, offset, limit));
    }

    /**
     * 客户登录
     *
     * @param account
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public BaseResult<Object> login(Account account) {
        // TODO
        return null;
    }

    /**
     * 更改客户资料
     *
     * @param account
     * @return
     */
    @RequestMapping(value = "/{accountId}", method = RequestMethod.PUT, produces = {"application/json;charset=UTF-8"})
    public BaseResult<Object> edit(Account account) {
        // TODO
        return null;
    }

    /**
     * 客户登出
     *
     * @param accountId
     * @return
     */
    @RequestMapping(value = "/{accountId}/logout", method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    public BaseResult<Object> logout(@PathVariable Integer accountId) {
        // TODO
        return null;
    }

    /**
     * 获得客户资料
     *
     * @return
     */
    @RequestMapping(value = "/{accountId}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Account getAccount(@PathVariable Integer accountId) {
        log.info("invoke----------/accounts/" + accountId + ".post");
        accountService.getAccountByTag(accountId, null, null);
        return null;
    }
}
